import pyautogui
from PIL import Image
import pytesseract
from openai import OpenAI

# 1️⃣ 初始化 OpenAI 客户端（替换为你的真实 API Key）
client = OpenAI(api_key="sk-xxxxxxxxxxxxxxxxxxxx")

# 2️⃣ 截图并保存
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")

# 3️⃣ OCR 识别中文文字
# 如果 Tesseract 提示找不到 chi_sim，确保安装了 tesseract-ocr-chi-sim 并设置 TESSDATA_PREFIX
# export TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata
text_in_image = pytesseract.image_to_string("screenshot.png", lang='chi_sim')

if not text_in_image.strip():
    print("未识别到任何文字！")
else:
    print("识别到的文字：", text_in_image)

    # 4️⃣ 调用 OpenAI 翻译
    prompt = f"请将下面的中文翻译成英文：\n{text_in_image}"

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )

    translated_text = response.choices[0].message.content
    print("翻译结果：", translated_text)
